package com.lframework.xingyun.basedata.controller;

import com.lframework.starter.web.core.annotations.security.HasPermission;
import com.lframework.starter.web.core.controller.DefaultBaseController;
import com.lframework.starter.web.core.utils.PageResultUtil;
import com.lframework.starter.web.core.components.resp.PageResult;
import com.lframework.starter.web.core.components.resp.InvokeResult;
import javax.validation.constraints.NotBlank;

import com.lframework.xingyun.basedata.bo.coupon.GetCouponBo;
import com.lframework.xingyun.basedata.bo.coupon.QueryCouponBo;
import com.lframework.xingyun.basedata.entity.Coupon;
import com.lframework.xingyun.basedata.service.coupon.CouponService;
import com.lframework.xingyun.basedata.vo.coupon.CreateCouponVo;
import com.lframework.xingyun.basedata.vo.coupon.QueryCouponVo;
import com.lframework.xingyun.basedata.vo.coupon.UpdateCouponVo;
import io.swagger.annotations.ApiImplicitParam;
import com.lframework.starter.web.core.components.resp.InvokeResultBuilder;
import com.lframework.starter.common.exceptions.impl.DefaultClientException;
import io.swagger.annotations.ApiOperation;
import com.lframework.starter.common.utils.CollectionUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 卡券信息 Controller
 *
 */
@Api(tags = "卡券信息")
@Validated
@RestController
@RequestMapping("/basedata/coupon")
public class CouponController extends DefaultBaseController {

    @Autowired
    private CouponService couponService;

    /**
     * 查询列表
     */
    @ApiOperation("查询列表")
    @HasPermission({
            "base-data:coupon:query",
            "base-data:coupon:prepaid:query",
            "base-data:coupon:counting:query",
    })
    @GetMapping("/query")
    public InvokeResult<PageResult<QueryCouponBo>> query(@Valid QueryCouponVo vo) {

        PageResult<Coupon> pageResult = couponService.queryPage(getPageIndex(vo), getPageSize(vo), vo);

        List<Coupon> datas = pageResult.getDatas();
        List<QueryCouponBo> results = null;

        if (!CollectionUtil.isEmpty(datas)) {
            results = datas.stream().map(QueryCouponBo::new).collect(Collectors.toList());
        }

        return InvokeResultBuilder.success(PageResultUtil.rebuild(pageResult, results));
    }

    /**
     * 根据ID查询
     */
    @ApiOperation("根据ID查询")
    @ApiImplicitParam(value = "id", name = "id", paramType = "query", required = true)
    @HasPermission({
            "base-data:coupon:query",
            "base-data:coupon:prepaid:query",
            "base-data:coupon:counting:query",
    })
    @GetMapping
    public InvokeResult<GetCouponBo> get(@NotBlank(message = "id不能为空！") String id) {

        Coupon data = couponService.findById(id);
        if (data == null) {
            throw new DefaultClientException("卡券信息不存在！");
        }

        GetCouponBo result = new GetCouponBo(data);

        return InvokeResultBuilder.success(result);
    }

    /**
     * 新增
     */
    @ApiOperation("新增")
    @HasPermission({
            "base-data:coupon:add",
            "base-data:coupon:prepaid:add",
            "base-data:coupon:counting:add",
    })
    @PostMapping
    public InvokeResult<Void> create(@Valid CreateCouponVo vo) {

        couponService.create(vo);

        return InvokeResultBuilder.success();
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @HasPermission({
            "base-data:coupon:modify",
            "base-data:coupon:prepaid:modify",
            "base-data:coupon:counting:modify",
    })
    @PutMapping
    public InvokeResult<Void> update(@Valid UpdateCouponVo vo) {

        couponService.update(vo);

        couponService.cleanCacheByKey(vo.getId());

        return InvokeResultBuilder.success();
    }
}

